﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ANN;

namespace NeuralOCR {
    class Program {
        // Test a simple AND neural net
        static void Main(string[] args) {
            // Create a new neural network
            ANN.FeedForward.Network NeuralNet = new ANN.FeedForward.Network(2, 1, 3, 3);

            // Create Teacher
            ANN.Learning.BackPropagation Teacher = new ANN.Learning.BackPropagation(NeuralNet);

            // Create template data
            float[][] templateData = new float[4][];
            templateData[0] = new float[] { 0, 0 };
            templateData[1] = new float[] { 0, 1 };
            templateData[2] = new float[] { 1, 0 };
            templateData[3] = new float[] { 1, 1 };

            // Create Optimal Output array
            float[][] optimalData = new float[4][];
            optimalData[0] = new float[] { 0 };
            optimalData[1] = new float[] { 1 };
            optimalData[2] = new float[] { 1 };
            optimalData[3] = new float[] { 1 };

            // Create testSet data
            float[][] testSetData = new float[8][];
            testSetData[0] = new float[] { 1, 0 };
            testSetData[1] = new float[] { 1, 1 };
            testSetData[2] = new float[] { 0, 0 };
            testSetData[3] = new float[] { 0, 0 };
            testSetData[4] = new float[] { 0, 0 };
            testSetData[5] = new float[] { 0, 1 };
            testSetData[6] = new float[] { 0, 0 };
            testSetData[7] = new float[] { 0, 1 };

            Console.WriteLine("Started Teaching network..");
            // Set the error limit and teach the network untill it converges
            Teacher.Limit = 0.5f;
            int epochCounter = 0;
            while (!Teacher.IsConverged) {
                float error = Teacher.LearnEpoch(templateData, optimalData);
                if (epochCounter++ % 100 == 0) {
                    Console.WriteLine(error);
                }
            }

            Console.WriteLine("Finished Teaching the network..");
            Console.ReadKey();

            float[][] Outputs = new float[testSetData.Length][];

            for (int i = 0; i < testSetData.Length; i++) {
                Outputs[i] = NeuralNet.Calculate(testSetData[i]);
            }

            for (int i = 0; i < Outputs.Length; i++) {
                Console.WriteLine("Output for set {0}", i);
                for (int j = 0; j < Outputs[i].Length; j++) {
                    Console.WriteLine(Outputs[i][j]);
                }
                Console.ReadKey();
            }
            Console.ReadKey();
        }
    }
}
